<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="50" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="150" align="center"></el-table-column>
<!--      <el-table-column prop="password" label="密码" width="200" header-align="center"></el-table-column>-->
      <el-table-column prop="nickname" label="昵称" width="200" align="center"></el-table-column>
<!--      <el-table-column prop="avatar" label="头像URL" width="200" align="center"></el-table-column>-->
      <el-table-column prop="phone" label="手机号" width="200" align="center"></el-table-column>
      <el-table-column prop="email" label="电子邮件" width="250" align="center"></el-table-column>
      <el-table-column prop="description" label="描述" width="300" align="center"></el-table-column>
      <el-table-column label="是否启用" width="130" align="center">
        <template slot-scope = "scope">
          <el-switch
                  @change="editEnable(scope.row)"
                  v-model="scope.row.enable"
                  :disabled="scope.row.id==1"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#AAAAAA">
          </el-switch>
        </template>
      </el-table-column>
<!--      <el-table-column prop="lastLoginIp" label="最后登录的IP地址" width="100" align="center"></el-table-column>-->
<!--      <el-table-column prop="loginCount" label="登录次数" width="50" align="center"></el-table-column>-->
<!--      <el-table-column prop="gmtLastLogin" label="最后一次登录时间" width="80" align="center"></el-table-column>-->
<!--      <el-table-column prop="gmtCreate" label="创建时间" width="120" align="center"></el-table-column>-->
<!--      <el-table-column prop="gmtModified" label="更新时间" width="120" align="center"></el-table-column>-->

      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     :disabled="scope.row.id == 1"  @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete"circle size="mini"
                     :disabled="scope.row.id == 1" @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: []
      }
    },
    methods: {
      editEnable(admin){
        let s = ['禁用','启用'];
        let url = 'http://localhost:9081/admins/'+admin.id;
        if(admin.enable==1){
          console.log("正在启用管理员");
          url += "/enable";
        }else{
          console.log("正在禁用管理员");
          url += "/disable";
        }
        console.log(url)
        this.axios.post(url).then((response) => {
          let responseBody = response.data;
          if(responseBody.state == 20000){
            let message = "管理员"+admin.username+s[admin.enable]+"成功";
            this.$message({
              message: message,
              type: 'success'
            })
          }else {
              this.$message.error(responseBody.message);
            }
          if (responseBody.state == 40400) {
            this.loadAdminList();
          }
        })

      },
      handleEdit(admin) {
        let message = '您正在尝试编辑【' + admin.id + '-' + admin.username + '】的管理员的详情，抱歉，该功能尚未实现……';
        this.$alert(message, '提示', {
          confirmButtonText: '确定'
        });
      },
      handleDelete(admin) {
        let url = 'http://localhost:9081/admins/' + admin.id + '/delete';
        console.log('url = ' + url);
        this.axios.post(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state != 20000) {
            this.$message.error(responseBody.message);
          }
          if (responseBody.state == 20000 || responseBody.state == 40400) {
            this.loadAdminList();
          }
        });
      },
      openDeleteConfirm(admin) {
        let message = '此操作将永久删除【' + admin.name + '】相册，是否继续？';
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(admin);
        }).catch(() => {
        });
      },
      loadAdminList() {
        console.log('loadAlbumList');
        let url = 'http://localhost:9081/admins';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.tableData = responseBody.data;
        });
      }
    },
    mounted() {
      console.log('mounted');
      this.loadAdminList();
    }
  }
</script>
